define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {  
                    type:3,
                    
                    user_info:{},
                    disabled:0,
                    page:1,
                    lastpage:0,
                    loading:0,
                    moneyLog:[],
                    myBanks:[],
                    payList:[],
                    payListSelect:[],
                    payListSelectKey:0,
                    payListSelectText:'请选择金额'
                };
            },
            el: '#pageMoney',
            created: function() {
                this.fetchData();
                
            },
            mounted: function() {
                    // console.log("123");
                    if (this.mounted == 1) {
                        return;
                    }
                    
                    var that = this ;
                    app.pager("#pageMoney",function(){
                       that.loadmoney();
                    });
                    this.mounted = 1;

            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.user_info= app.getUserInfo();
                     this.getInfo();
                     this.loadmoney();
                    //充多少送多少
                    this.getPayList();
                    
                },
                getInfo:function(){
                    var that = this;
                    app.get("api/user/info", {
                      user_id:this.user_info.user_id,
                      fields:"avatarUrl,user_id,last_update_time,nickname"
                    }, function (res) {
                      
                      if (res.code != 200) {
                        app.alert(res.msg);
                        return;
                      }
                       that.income=(res.data.income*0.99).toFixed(2);
                       that.fee = (res.data.income * 0.01).toFixed(2);
                       that.user_info=res.data;
                      
                    });  
                  },
                //充值选项 
                  getPayList:function(){
                    var that = this;
                    app.get("mall/promotion/find", {
                      name: 'recharge'
                    }, function (data) {
                      if (data.code != 200) {
                        app.alert(data.msg);
                        return;
                      }
                      //console.log(data.data.name);
                      var payListSelect =[];
                      for (var i in data.data.config.set){
                        payListSelect.push("充" + data.data.config.set[i].recharge + "送" + data.data.config.set[i].present);
                      }
                      that.payList=data.data;
                      that.payListSelect=payListSelect;

                    });
                  },
                   unDoWithDraw:function(){
                    this.setViweType(3);
                    //setViweType(3)
                  },
                  //资金流水 
                  loadmoney:function(reload=0){
                    var that = this;
                    
                    if (reload==1){
                        this.loading=0;
                        this.page=1;
                        this.lastpage=0;
                        this.moneyLog=[];
                      
                    }
                    
                    if (this.loading==1 || this.lastpage==1){
                      return ;
                    }
                    var page = this.page;
                    this.loading=1;
                    app.get("api/money_log/get", {
                      page: page,
                      limit:10,
                      order:'id desc'
                    },function(res){
                      that.loading = 0;
                      if (res.code != 200) {
                        app.alert(res.msg);
                        return;
                      }
                      if (res.data.list.length===0){
                        that.lastpage=1;
                      }
                      that.page++;
                      that.moneyLog = that.moneyLog.concat(res.data.list);
                    });
                  },
                 //充值
                  doRecharge:function(){

                    var that = this;
                    if(this.loading==1 ||  this.disabled==1){
                        return ;
                    }
                    this.loading=1;
                    this.disabled=1;
                    var recharge_id = $('[name="recharge_id"]:checked').val();
                    if(typeof(recharge_id)=="undefined"){
                        app.alert("请选择充值金额");
                        return;
                    }
                    app.get("/api/pay/recharge", {
                      recharge_id: recharge_id,
                    }, function (res) {
                      if (res.code != 200) {
                        app.alert(res.msg);
                        that.loading=0;
                        that.disabled=0;
                        return false;
                      }
                      var order_sn = res.data.order_sn;
                      app.pay(order_sn);
                    });
                  },

                  showView:function(type){
                    this.setViweType(type );
                    
                  },
                  setViweType:function(id){
                    this.type = id;
                  }
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
